﻿@inherits BaseComponent

<FieldItem Label="@Label">
    @if (Data.ContainsKey(Item.Id))
    {
        if (!string.IsNullOrWhiteSpace(Item.Category))
        {
            var codes = Cache.GetCodes(Item.Category).ToCodes(Item.IsQueryAll ? Language["All"] : "");
            <AntSelectCode Style="@SelectStyle" DataSource="codes" Value="@Data[Item.Id].Value" ValueChanged="v=>OnSelectChanged(Item.Id,v)" />
        }
        else if (itemType.Contains("DateTime") || Item.Type == FieldType.Date || Item.Type == FieldType.DateTime)
        {
            <AntRangePicker @bind-RangeValue="@Data[Item.Id].Value" />
        }
        else if (itemType.Contains("Boolean") || Item.Type == FieldType.Switch || Item.Type == FieldType.CheckBox)
        {
            var codes = new List<CodeInfo>();
            codes.Add(new CodeInfo("", Language["All"]));
            codes.Add(new CodeInfo("True", Language["Yes"]));
            codes.Add(new CodeInfo("False", Language["No"]));
            <AntSelectCode Style="@SelectStyle" DataSource="codes" Value="@Data[Item.Id].Value" ValueChanged="v=>OnSelectChanged(Item.Id,v)" />
        }
        else
        {
            <AntInput Value="@Data[Item.Id].Value" ValueChanged="v=>Data[Item.Id].Value=v" OnPressEnter="e=>OnSearch()" />
        }
    }
</FieldItem>

@code {
    private string itemType = "String";
    private string SelectStyle => IsInline ? "width:194px;" : "";

    [Parameter] public string Label { get; set; }
    [Parameter] public bool IsInline { get; set; }
    [Parameter] public ColumnInfo Item { get; set; }
    [Parameter] public Dictionary<string, QueryInfo> Data{ get; set; }
    [Parameter] public Func<Task> OnSearch { get; set; }

    protected override Task OnInitAsync()
    {
        if (Item.Property != null)
            itemType = Item.Property.PropertyType.ToString();
        return base.OnInitAsync();
    }

    private async void OnSelectChanged(string id, string value)
    {
        if (value == Data[id].Value)
            return;

        Data[id].Value = value;
        await OnSearch?.Invoke();
    }
}